﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PadnugDomain = padnug.Domain;
using PadnugService;
using padnug.Models.Home;

namespace padnug.Controllers
{
    public class HomeController : BaseController
    {
        private IPadnugCalendarService _calendarService;
        private IPadnugEventsService _eventService;
        private IPadnugSponsorService _sponsorService;
        public HomeController() : this(new PadnugCalendarService(), new PadnugEventsService(), new PadnugSponsorService())
        {

        }

        public HomeController(IPadnugCalendarService calendarService, IPadnugEventsService padnugEventsService, IPadnugSponsorService padnugSponsorService)
        {
            _calendarService = calendarService;
            _eventService = padnugEventsService;
            _sponsorService = padnugSponsorService;
        }

        public ActionResult Index()
        {
            var featuredPresentation = GetFeaturedEvent();
            var upcomingPresentations = GetUpcomingEvents();
            var featuredSponsors = GetPremimumSponsors();
            var eventNotes = GetEventNotes();

            var model = new IndexViewModel
            {
                FeaturedPresentation = featuredPresentation,
                UpcomingPresentations = upcomingPresentations,
                FeaturedSponsors = featuredSponsors,
                EventNotes = eventNotes
            };

            var eventDates = _calendarService.GetAllEventDates();

            ViewData["EventDates"] = eventDates;
            return View(model);
        }

        public ActionResult About()
        {
            ViewData["FeaturedSponsors"] = GetPremimumSponsors();
            ViewData["EventNotes"] = GetEventNotes();
            return View();
        }

        public ActionResult GetCalendarEvents()
        {
            IList<PadnugDomain.CalendarEvent> events = new List<PadnugDomain.CalendarEvent>();

            if (System.Web.HttpContext.Current.Session["CalendarEvents"] == null)
            {
                events = _calendarService.GetAllEvents();
                System.Web.HttpContext.Current.Session.Add("CalendarEvents", events);
            }
            else
            {
                events = (List<PadnugDomain.CalendarEvent>)System.Web.HttpContext.Current.Session["CalendarEvents"];
            }
            return Json(events);
        }

        public ActionResult EventCalendar(long? dt)
        {
            DateTime dateTime = dt.HasValue ? new DateTime(dt.Value) : DateTime.Now;
            var events = _calendarService.GetMonthsEvents(dateTime);
            return Json(events);
        }

        public ActionResult EventsForADate(long? dt)
        {
            DateTime eventDate = dt.HasValue ? new DateTime(dt.Value) : DateTime.Now;
            var events = _eventService.GetEventsForADate(eventDate);
            return Json(events);
        }

        private IEnumerable<Sponsor> GetPremimumSponsors()
        {
            var list = new List<Sponsor>();

            var padnugPremiumSponsors = _sponsorService.GetPremimumSponsors();

            foreach (var p in padnugPremiumSponsors)
            {
                Sponsor s = new Sponsor { LogoUrl = p.SponsorLargeImage, Name = p.SponsorName, Website = p.SponsorURL };
                list.Add(s);
            }

            return list;
        }

        private Sponsor GetPremiumSponsor()
        {
            var sponsor = new Sponsor
            {
                LogoUrl = "/content/images/microsoft.png",
                Name = "Microsoft, Inc",
                Website = "http://www.microsoft.com"
            };

            return sponsor;
        }

        private IEnumerable<Presentation> GetUpcomingEvents()
        {
            var list = new List<Presentation>();

            IList<PadnugDomain.Event> eventsList = _eventService.GetUpcomingEvents();
            foreach (var e in eventsList)
            {
                var p = new Presentation
                {
                    EventDate = e.MeetingStart.HasValue ? e.MeetingStart.Value : DateTime.Now,
                    OrganizationName = e.OrganizationName,
                    SpeakerName = e.SpeakerName,
                    SpeakerPhotoUrl = e.SpeakerPhotoUrl,
                    Title = e.Topic
                };

                list.Add(p);
            }

            return list;
        }

        private Presentation GetFeaturedEvent()
        {
//            var featuredPresentation = new Presentation
//            {
//                EventDate = DateTime.Now.AddDays(5),
//                OrganizationName = "PADNUG",
//                SpeakerName = "Alice Brown",
//                SpeakerPhotoUrl = "/content/images/andrew-thumb.png",
//                Title = "What's New in MVC 2",
//                EventDetailsHtml = @"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
//                                    <p>
//                                        <strong>Time:</strong><br />
//                                        6:00pm Pizza (by Ascentium)<br />
//                                        6:30pm Presentation<br />
//                                        9:00pm Afterwards at Gustav's
//                                    </p>
//                                    <p>
//                                        <strong>Location:</strong><br />
//                                        Microsoft Portland Office located in Lincoln Tower<br />
//                                        10260 SW Greenburg Road<br />
//                                        Suite 600<br />
//                                        Portland, OR 97223
//                                    </p>"
//            };

            PadnugDomain.Event e = _eventService.GetFeaturedEvent();
            var featuredPresentation = new Presentation
            {
                EventDate = e.MeetingStart.HasValue ? e.MeetingStart.Value : DateTime.Now,//DateTime.Now.AddDays(5),
                OrganizationName = e.OrganizationName,
                SpeakerName = e.SpeakerName,
                SpeakerPhotoUrl = e.SpeakerPhotoUrl,
                Title = e.Topic//,
                //   EventDetailsHtml = @"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                //<p>
                //    <strong>Time:</strong><br />
                //    6:00pm Pizza (by Ascentium)<br />
                //    6:30pm Presentation<br />
                //    9:00pm Afterwards at Gustav's
                //</p>
                //<p>
                //    <strong>Location:</strong><br />
                //    Microsoft Portland Office located in Lincoln Tower<br />
                //    10260 SW Greenburg Road<br />
                //    Suite 600<br />
                //    Portland, OR 97223
                //</p>"
            };

            return featuredPresentation;
        }

        private IEnumerable<EventNote> GetEventNotes()
        {
            var list = new List<EventNote>();

            for (int i = 0; i < 3; i++)
            {
                var eventNote = new EventNote
                {
                    Title = "OTN: Certified Scrum Master Training",
                    EventDate = "April 22 and April 23, 9:00am-5:00pm",
                    Url = "http://www.sao.org/events/event_details.asp?id=102320"
                };

                list.Add(eventNote);
            }

            return list;
        }
    }
}
